package com.lagou.transfer.servlet;

import com.alibaba.fastjson.JSONObject;
import com.lagou.transfer.pojo.Bank;
import com.lagou.transfer.pojo.dto.TransferDto;
import com.lagou.transfer.service.BankService;
import com.spring.factory.AbstractBeanFactory;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet(urlPatterns = "/bank")
public class BankServlet extends HttpServlet {
    private BankService bankService;

    @Override
    public void init() throws ServletException {
        bankService = AbstractBeanFactory.getAbstractBeanFactory().getBean("bankServiceImpl");
        System.out.println(bankService);
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("application/json;charset=utf-8");
        PrintWriter writer = response.getWriter();
        List<Bank> bankList = bankService.select();
        String jsonString = JSONObject.toJSONString(bankList);
        writer.println(jsonString);
        // 查询
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 编辑 转账操作
        String money = request.getParameter("money");
        String remitterCardNo = request.getParameter("remitterCardNo");
        String payeeCardNo = request.getParameter("payeeCardNo");
        response.setContentType("text/html;charset=utf-8");
        PrintWriter writer = response.getWriter();
        try {
            TransferDto transferDto = new TransferDto(remitterCardNo,payeeCardNo,Float.valueOf(money));
            bankService.transfer(transferDto);
            writer.print("转账成功！");
        }catch (Exception e){
            writer.print("转账失败！");
        }

    }

}
